#include <SFML/Graphics/RenderWindow.hpp>
#include <nice2d/macros.h>
#include <nice2d/scene.h>
#include <nice2d/background.h>
#include <nice2d/imagedescriptor.h>
#include <nice2d/components/group.h>
#include <nice2d/components/spritestatus.h>
#include <nice2d/components/imagemanager.h>
#include <nice2d/util.h>
#include <nice2d/tilemap.h>
#include <nice2d/piece/piecescene.h>
#include <nice2d/piece/factionmanager.h>

#define SCENE_WIDTH  900.0f
#define SCENE_HEIGHT 680.0f

#define OFFSET 10

class UnitScene : public PieceScene
{
public:
    void onInitialed()override
    {
        auto factionManager = GET_APPLICATION_COMPONENT(FactionManager);
        factionManager->createFaction(sf::Color::Red,L"red");
        factionManager->createFaction(sf::Color::Green,L"green");

        setSceneBox({0.0f, 0.0f, SCENE_WIDTH, SCENE_HEIGHT});
        setBackground(Background::create("./../data/images/ground3.png", true));

        tileMap = TileMap::create(TileMapType::TileMapType_Hex);
        tileMap->init(13,9,96);
        tileMap->setTextVisible(true);

        tileMap->accept([](int32_t x, int32_t y, std::shared_ptr<Tile> tile)
        {
            tile->setOutlineColor(sf::Color(128,128,128));
            tile->setOutlineThickness(0.5f);
        });

        addChild(tileMap);
        setTileMap(tileMap);
        tileMap->tileClicked.connect(std::bind(&UnitScene::tileClicked,this,std::placeholders::_1,std::placeholders::_2));

        TextOption opt;
        opt.characterSize = 15;

        sf::Color spriteColor[] = {sf::Color::Red,sf::Color::Green};

        for(int i=0;i<12;i++)
        {
            auto piece = std::make_shared<Piece>(opt);
            piece->setHitPoint(3);
            piece->setAttack(2);
            piece->setDefense(5);
            piece->setMovement(4);
            piece->setBackgroundColor(sf::Color(140,140,140));
            piece->setSpriteColor(spriteColor[rand()%2]);
            piece->setSize(64,64);
            piece->addPieceIcon("./../data/icon/nato/units/infantry.png");
            piece->addPieceIcon("./../data/icon/nato/sizes/group.png");
            piece->setTextColor(sf::Color::White);
            addPiece(piece,rand()%13,rand()%9);

            piece->selected.connect(std::bind(&UnitScene::spriteSelected,this,std::placeholders::_1));
        }
    }

    void tileClicked(int32_t x,int32_t y)
    {
        auto tile = tileMap->getTileByIndex(x,y);
        auto sprite = tile->getSprite();
        if(sprite)
            sprite->select();
    }

    void spriteSelected(SpritePointer sprite)
    {
        auto visitor = std::make_shared<PieceVisitor>();
        acceptPieces(visitor);

        sprite->setBackgroundColor(sf::Color(180,180,180));

        auto pieces = visitor->getPieces();
        std::for_each(pieces.begin(),pieces.end(),[&](std::shared_ptr<Piece> piece)
        {
            if(std::dynamic_pointer_cast<Piece>(sprite) != piece)
            {
                piece->unselect();
                piece->setBackgroundColor(sf::Color(140,140,140));
            }
        });
    }
private:
    std::shared_ptr<TileMap> tileMap;
};

DECLARE_MAIN_FUNCTION_WITH_SCENE(SCENE_WIDTH, SCENE_HEIGHT, "unit15", UnitScene)
